package cn.wolfcode.esdemo.service.impl;

import cn.wolfcode.esdemo.domain.Product;
import cn.wolfcode.esdemo.repository.ProductRepository;
import cn.wolfcode.esdemo.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ProductServiceImpl implements IProductService {

    //基本操作
    @Autowired
    private ProductRepository repository;

    //复杂操作
    @Autowired
    private ElasticsearchTemplate template;

    @Override
    public void save(Product product) {
        repository.save(product);
    }

    //全量更新
    @Override
    public void update(Product product) {
        repository.save(product);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Product get(String id) {
        Optional<Product> optional = repository.findById(id);
        return optional.get();
    }

    @Override
    public List<Product> list() {
        Iterable<Product> all = repository.findAll();
        List<Product> p = new ArrayList<>();
        all.forEach(a->p.add(a));
        return p;
    }
}
